{% extends 'layout.html' %}
{% block jquerypluginlib %}
    <script type="text/javascript" src="{{ url_for('static',filename='lib/jquery.fix.clone.js') }}"></script>
    <link rel="stylesheet" href="{{ url_for('static',filename='lib/jquery.validationEngine/css/validationEngine.jquery.css') }}" type="text/css">
    <script type="text/javascript" src="{{ url_for('static',filename='lib/jquery.validationEngine/js/jquery.validationEngine.js') }}"></script>
    <script type="text/javascript" src="{{ url_for('static',filename='lib/jquery.validationEngine/js/languages/jquery.validationEngine-zh_CN.js') }}"></script>
{% endblock jquerypluginlib %}
{% block main %}
    <script type=text/javascript>
    $(function() {
        $("form").validationEngine('attach',{
            validationEventTrigger:'submit'
        });
        $("#copy").click(function(){
            var $last_row=$("#tb_dizhi tbody tr:last");
            console.log($last_row);
            $("#tb_dizhi").find("tbody").append($last_row.clone());
        });
        function fillres(ress,$target){

            var optionstr='';
            if($target.attr("name")=="amp"){
                var optionstr='<option value="">无</option>';
            }
            $.each(ress,function(i,item){
                optionstr+='<option value="'+item.ResID+'">'+item.InstallAddress+' --- '+item.DeviceName+'</option>';
            });
            console.log(optionstr);
            console.log($target);
            $target.html(optionstr);
        }

        function filltbres(ress){
            var tr='';
            $.each(ress,function(i,item){
                tr+='<tr><td>'+(i+1)+'</td><td>'+item.ResID+'</td><td><a href="/res/'+item.ResID+'" target="_blank">'+item.InstallAddress+'</a></td><td>'+item.OSPort+'</td><td>'+item.DeviceName+'</td><td>'+item.Floor+'</td><td>'+item.Door+'</td><td>'+item.AddrCateName+'</td><td><a href="/res/'+item.ParentRes+'" target="_blank">'+item.pInstallAddress+' - '+item.pDeviceName+'</a></td><td>'+item.Memo+'</td></tr>';

            });
            $("#tb_ress tbody").html(tr);

        }


        $("select[name='comm']").live('change',function(){
            var $os=$(this).closest('tr').find('select[name="os"]');
{#            var $amp=$(this).closest('tr').find('select[name="amp"]');#}
            console.log($os);

            $.getJSON("/_json_os",{commid:$(this).val()},function(data){
                fillres(data.result,$os);
                $("select[name='os']").trigger('change');
            });
{#            $.getJSON("/_json_amp",{commid:$(this).val()},function(data){#}
{#                fillres(data.result,$amp);#}
{#            });#}
        });
        $("select[name='os']").live('change',function(){
            var $amp=$(this).closest('tr').find('select[name="amp"]');

            $.getJSON("/_json_amp",{resid:$(this).val()},function(data){
                fillres(data.result,$amp);
            });
            $.getJSON("/_json_get_allres7",{resid:$(this).val()},function(data){
                filltbres(data.result);
            });


        });
        $("select[name='comm']").trigger('change');




    });
    </script>
            <form method="post">
                <table id="tb_dizhi">
                    <thead>
                    <tr>
                        <th>小区</th>
                        <th>光站</th>
                        <th>输出端口</th>
                        <th>上级放大器</th>
                        <th>覆盖地址(标准地址)</th>
                        <th>楼层数</th>
                        <th>层户数</th>
                        <th>地址类型</th>
                        <th>备注</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>
                            <select name="comm">
                                {% for comm in comms %}
                                    <option value="{{ comm.CommID }}">{{ comm.CommName }}</option>
                                {% endfor %}
                            </select>
                        </td>
                        <td>
                            <select name="os"></select>
                        </td>
                        <td>
                            <select name="osport">
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                            </select>
                        </td>
                        <td><select name="amp"></select></td>
                        <td><input name="address" class="validate[required]" /></td>
                        <td><input name="floor" size="2" class="validate[required,custom[positiveinteger]]" /></td>
                        <td><input name="door" size="2" class="validate[required,custom[positiveinteger]]" /></td>
                        <td>
                            <select name="addrcate">
                                <option value="1">普通</option>
                                <option value="2">店面</option>
                            </select>
                        </td>
                        <td><input name="memo" /></td>
                    </tr>
                    </tbody>
                </table>
                <input id="copy" type="button" value="复制前一行" />
            <input type="submit" value="保存" />
            </form>
                <table id="tb_ress" class="bordertable">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>资源编号</th>
                        <th>安装地址</th>
                        <th>光站端口</th>
                        <th>资源类型</th>
                        <th>楼层数</th>
                        <th>层户数</th>
                        <th>地址类型</th>
                        <th>上级资源</th>
                        {#                <th>上级资源地址</th>#}
                        <th>备注</th>
                    </tr>
                    </thead>
                    <tbody>
                    {#            {% for res in ress %}#}
                    {#            <tr>#}
                    {#                <td>{{ loop.index }}</td>#}
                    {#                <td>{{ res.InstallAddress }}</td>#}
                    {#                <td>{{ res.DeviceType }}-{{ res.DeviceName }}</td>#}
                    {#                <td>{{ res.AssetNumber }}</td>#}
                    {#                <td>{{ res.SN }}</td>#}
                    {#                <td>{{ res.MAC }}</td>#}
                    {#                <td>{{ res.ParentRes }}</td>#}
                    {#                <td>{{ res.Memo }}</td>#}
                    {#            </tr>#}
                    {#            {% endfor %}#}
                    </tbody>
                </table>

{% endblock main %}
